﻿using System.Net;
using System.Net.Mail;
using DataManageSystem.Models;
using DataManageSystem.Utils;
using Microsoft.Extensions.Options;

namespace DataManageSystem.Services
{
    public class MailService : IMailService
    {
        private readonly MailSettings _settings;

        public MailService(IOptions<MailSettings> options)
        {
            _settings = options.Value;
        }


        public async Task SendEmailAsync(MailRequest request)
        {
            var smtpClient = new SmtpClient(_settings.Host)
            {
                Port = _settings.Port,
                Credentials = new NetworkCredential(_settings.Mail, _settings.Password),
                EnableSsl = true
            };
            var mail = new MailMessage
            {
                From = new MailAddress(_settings.Mail, _settings.DisplayName),
                Subject = request.Subject,
                Body = request.Body,
                IsBodyHtml = true
            };
            mail.To.Add(request.To);
            await smtpClient.SendMailAsync(mail);

        }
    }
}
